import time

import allure
from selenium.webdriver.common.by import By

from bazhuayun.page.base_octopus_cloud import BaseOctopusCloud
from bazhuayun.utils.log_utils import logger


class Commodity(BaseOctopusCloud):
    _CLICK_COMMODITY = By.XPATH, '(//*[text()="商品管理"])[1]'
    _CLICK_COMMODITY_LIST = By.XPATH, '(//*[text()="商品列表"])[1]'
    _CLICK_ADD = By.XPATH, '(//*[text()="添加"])[1]'
    _CLICK_GOODS_NAME = By.XPATH, '(//*[@class="el-input__inner"])[1]'
    _CLICK_FOUND = By.XPATH, '//*[text()="查询"]'
    _CLICK_TO_TAKE_SOMETHING_DOWN = By.XPATH, '//*[text()="下架"]'
    _CLICK_SUBMIT = By.XPATH, '//*[text()="提交"]'
    _CONFIRM_TEXT = By.XPATH, '//*[text()="操作成功"]'
    _CLICK_SOMETHING_DOWN_BTN = By.XPATH, '//*[text()="已下架商品"]'
    _GOODS_NAME = By.XPATH, '(//*[@class="el-input__inner"])[28]'
    _FOUND = By.XPATH, '(//*[text()="查询"])[3]'
    _CLICK_SHELVES = By.XPATH, '(//*[text()="上架"])'
    _SUBMIT_BTN = By.XPATH, '//*[text()="提交"]'
    _SUBMIT_TEXT = By.XPATH, '(//*[text()="操作成功"])'

    @allure.step("进入商品添加页面")
    def commodity(self):
        # 点击商品管理
        self.find_ele(*self._CLICK_COMMODITY).click()
        # 商品列表
        self.find_ele(*self._CLICK_COMMODITY_LIST).click()
        # 添加
        self.find_ele(*self._CLICK_ADD).click()
        logger.info("进入商品添加页面")
        from bazhuayun.page.commodity_add_page import CommodityAddPage
        return CommodityAddPage(self.driver)

    @allure.step("查找商品,下架商品")
    def found_commodity(self, name):
        # 点击商品名称
        time.sleep(2)
        self.find_ele(*self._CLICK_GOODS_NAME).click()
        self.find_ele(*self._CLICK_GOODS_NAME).send_keys(name)
        # 点击查询
        time.sleep(2)
        self.find_ele(*self._CLICK_FOUND).click()
        # 点击下架
        time.sleep(2)
        self.find_ele(*self._CLICK_TO_TAKE_SOMETHING_DOWN).click()
        # 点击提交
        self.find_ele(*self._CLICK_SUBMIT).click()
        # 确认文本
        time.sleep(2)
        res = self.find_ele(*self._CONFIRM_TEXT)
        logger.info("查找商品,并下架")
        return res

    @allure.step("查找商品,上架商品")
    def shangjia_shangpin(self, name):
        # 点击商品管理
        self.find_ele(*self._CLICK_COMMODITY).click()
        # 商品列表
        self.find_ele(*self._CLICK_COMMODITY_LIST).click()
        # 点击已下架
        self.find_ele(*self._CLICK_SOMETHING_DOWN_BTN).click()
        # 点击商品名称
        time.sleep(2)
        self.find_ele(*self._GOODS_NAME).click()
        time.sleep(2)
        self.find_ele(*self._GOODS_NAME).send_keys(name)
        # 点击查询
        self.find_ele(*self._FOUND).click()
        # 点击上架
        self.delete_channel_qiangzhi(*self._CLICK_SHELVES)
        # self.find_ele(*self._CLICK_SHELVES).click()
        # 点击确定
        self.find_ele(*self._SUBMIT_BTN).click()
        # 确认文本
        res = self.find_ele(*self._SUBMIT_TEXT)
        logger.info("查找商品,并上架")
        return res











